﻿using EMoney.IStock.Client.Core;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.QQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThinkDev.Logging;

namespace EMoney.IStock.Client.Business.QQ
{
    /// <summary>
    /// 订阅通知业务处理类
    /// </summary>
    public class MsgBll
    {//自由人|2014-10-12 14:13:37|买入|sz000001|平安银行|100|10.15
        /// <summary>
        /// 接收腾讯通知的订阅信息
        /// </summary>
        /// <param name="from_uin"></param>
        /// <param name="token"></param>
        /// <param name="msg">高手名称|买卖方向|交易时间|股票代码|股票名称|股票数量|交易价格</param>
        /// <param name="to_qq"></param>
        /// <param name="timestamp"></param>
        public void ReceiveMsg(string from_uin, string token, string msg, string to_qq, string timestamp)
        {
            string[] arrMsg = msg.Split('|');

            if (arrMsg.Length == 7)
            {
                string key = string.Empty;
                string[] arrToQQ = to_qq.Split(';');

                string code = StringHelper.FormatQQStockCode(arrMsg[3]);

                if (!code.StartsWith("6"))
                {
                    code = "1" + code;
                }

                long msgid = long.Parse(StringHelper.FormatDateTime(DateTime.Now, "MMddHHmmssfff"));

                foreach (string qq in arrToQQ)
                {
                    try
                    {
                        Msg objMsg = new Msg();

                        objMsg.msgid = msgid;
                        objMsg.nick = arrMsg[0];

                        if (arrMsg[1] == "买入")
                        {
                            objMsg.buysell = "模拟买入";
                        }
                        else if (arrMsg[1] == "卖出")
                        {
                            objMsg.buysell = "模拟卖出";
                        }

                        objMsg.time = arrMsg[2];
                        objMsg.code = code;
                        objMsg.name = arrMsg[4];
                        objMsg.num = arrMsg[5];
                        objMsg.price = arrMsg[6];
                        objMsg.flag = 0;
                        objMsg.type = "腾讯炒股大赛";

                        key = string.Concat(QQConstDefine.RSSMSG_CACHED_KEY, ":", qq, ":", StringHelper.FormatDateTime(TradeDateBll.GetTradeDate()));

                        List<Msg> list = CacheManager.QQCache.Get<List<Msg>>(key);

                        if (list == null)
                        {
                            list = new List<Msg>();
                        }

                        list.Add(objMsg);

                        CacheManager.QQCache.Set<List<Msg>>(key, list, 60 * 24 * 3);
                    }
                    catch (Exception ex)
                    {
                        LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("接收腾讯通知的订阅信息失败"));
                    }                    
                }
            }
        }

        /// <summary>
        /// 获取订阅信息
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="msgid"></param>
        /// <returns></returns>
        public List<Msg> GetMsg(string qq, long msgid)
        {
            List<Msg> list = null;

            try
            {
                string key = string.Concat(QQConstDefine.RSSMSG_CACHED_KEY, ":", qq, ":", StringHelper.FormatDateTime(TradeDateBll.GetTradeDate()));

                list = CacheManager.QQCache.Get<List<Msg>>(key);

                if (list != null && list.Count > 0)
                {
                    list = list.FindAll(lst => lst.msgid > msgid);
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("获取订阅信息失败.", qq, ".", msgid));
            }

            return list;
        }
    }
}
